package cn.wlinker.video.monitor.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.wlinker.video.monitor.config.DefaultConfig;
import cn.wlinker.video.monitor.domain.Page;
import cn.wlinker.video.monitor.domain.PageQuery;
import cn.wlinker.video.monitor.domain.R;
import cn.wlinker.video.monitor.domain.dto.*;
import cn.wlinker.video.monitor.domain.vo.*;
import cn.wlinker.video.monitor.service.impl.CommonVideoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 监控设备控制器
 *
 * @author gxsjx
 * @version 1.0
 * @date 2022/7/27
 */
@Api(tags = "监控设备管理")
@RestController
@RequestMapping("/api/monitorDevice")
public class MonitorDeviceController {

    private final CommonVideoServiceImpl commonVideoServiceImpl = CommonVideoServiceImpl.INSTANCE;

    @Resource
    private DefaultConfig defaultConfig;

    @ApiOperation(value = "查询网络设备树")
    @ApiImplicitParam(name = "monitorType",value = "监控品牌",allowableValues = "hk,dh,ys")
    @GetMapping("/getNetDeviceTree")
    public R<List<Tree<String>>> getNetDeviceTree(String monitorType) {
        if(StrUtil.isBlank(monitorType)){
            monitorType = defaultConfig.getMonitorType();
        }
        return R.data(commonVideoServiceImpl.getNetDeviceTree(monitorType));
    }

    @ApiOperation(value = "查询设备列表")
    @GetMapping("/getList")
    public R<Page<MonitorDeviceVO>> getList(MonitorDeviceDTO monitorDeviceDTO, PageQuery pageQuery) {
        return R.data(commonVideoServiceImpl.getMonitorDeviceList(monitorDeviceDTO, pageQuery));
    }

    @ApiOperation(value = "获取视频流")
    @GetMapping("/getUrl")
    public R<VideoVO> getVideoUrl(PlayUrlDTO playUrlDTO) {
        return R.data(commonVideoServiceImpl.getUrl(playUrlDTO));
    }

    @ApiOperation(value = "根据id集合获取视频流")
    @GetMapping("/getUrls")
    public R<List<VideoVO>> getUrls(@RequestBody List<String> ids, PlayUrlDTO playUrlDTO) {
        return R.data(commonVideoServiceImpl.getUrls(ids, playUrlDTO));
    }

    @ApiOperation(value = "开始云台转动")
    @PostMapping("/ptcStart")
    public R<Boolean> ptcStart(@RequestBody PtzControlDTO ptzControlDTO) {
        //开始之前先停止转动
        commonVideoServiceImpl.ptzStop(ptzControlDTO);
        Boolean aBoolean = commonVideoServiceImpl.ptzStart(ptzControlDTO);
        return R.status(aBoolean);
    }

    @ApiOperation(value = "停止云台转动")
    @PostMapping("/ptcStop")
    public R<Boolean> ptcStop(@RequestBody PtzControlDTO ptzControlDTO) {
        Boolean aBoolean = commonVideoServiceImpl.ptzStop(ptzControlDTO);
        return R.status(aBoolean);
    }

    @ApiOperation(value = "图片抓拍")
    @PostMapping("/capture")
    public R<PictureUrlVO> capture(@RequestBody CaptureDTO captureDTO) {
        PictureUrlVO pictureUrlVO = commonVideoServiceImpl.capture(captureDTO);
        return R.data(pictureUrlVO);
    }

    @ApiOperation(value = "添加预置点")
    @PostMapping("/presetAdd")
    public R<PresetAddVO> presetAdd(PresetDTO presetDTO) {
        //设置默认预置点索引为1
        if (presetDTO.getIndex() == null) {
            presetDTO.setIndex(1);
        }
        PresetAddVO pictureUrlVO = commonVideoServiceImpl.presetAdd(presetDTO);
        return R.data(pictureUrlVO);
    }

    @ApiOperation(value = "调用预置点")
    @PostMapping("/presetMove")
    public R<Boolean> presetMove(@RequestBody PresetDTO presetDTO) {
        Boolean aBoolean = commonVideoServiceImpl.presetMove(presetDTO);
        return R.status(aBoolean);
    }

    @ApiOperation(value = "清除预置点")
    @PostMapping("/presetClear")
    public R<Boolean> presetClear(@RequestBody PresetDTO presetDTO) {
        Boolean aBoolean = commonVideoServiceImpl.presetClear(presetDTO);
        return R.status(aBoolean);
    }


    @ApiOperation(value = "查询设备能力集", notes = "响应结果中没有相关字段则默认不支持,support_ptz:是否支持云台控制;ptz_preset:是否支持云台预置点;")
    @GetMapping("/capacity")
    public R<JSONObject> capacity(String id) {
        JSONObject capacity = commonVideoServiceImpl.capacity(id);
        return R.data(capacity);
    }

    @ApiOperation(value = "获取视频回放流")
    @GetMapping("/getRelayUrl")
    public R<VideoVO> getRelayUrl(PlayUrlDTO playUrlDTO) {
        VideoVO relayUrl = commonVideoServiceImpl.getRelayUrl(playUrlDTO);
        return R.data(relayUrl);
    }
}
